<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/6/21
 * Time: 上午10:07
 */

namespace app\index\model;


use app\index\service\CategoryService;
use app\index\service\QuotaService;
use think\Model;

class Quota extends Model
{
    protected $table = "t_quota";
    public function staffitems()
    {
        return $this->belongsToMany('StaffFee','t_quotastaff',"staff_fee_id","quota_id");

    }
    public static function getLevelsByCateId($cateId){
        $data = self::where('subcategory_id', '=', $cateId)
            ->order('level asc')
            ->select();
        if($data->isEmpty()){
            return [];
        }
        $levels = [];
        foreach ($data as $item){
            $tmp["level_id"] = $item['level'];
            $tmp["level_name"] = Level::getLevelNameById($item['level']);
            array_push($levels,$tmp);
        }
        return $levels;
    }
    public static function getQuotaListById($supcateid,$subcateId){
        $data = self::where('supcategory_id', '=', $supcateid)
            ->where('subcategory_id','=',$subcateId)
            ->order('level asc')
            ->select();
        return $data;
    }
    public static function getLastId($id){
        $last_id = self::where('id', '<', $id)->max('id');
        return $last_id;
    }
    public static function getNextId($id){
        $next_id = self::where('id', '>', $id)->min('id');
        return $next_id;
    }
    public static  function getOne($id){
        $data = self::find($id);
        return $data;
    }
    public static function getRecList($num=2){
        $recommend = self::where("flag","=",1)
            ->order('rand()')->limit(0,$num)->select();
        $rec_list = [];
        foreach ($recommend as &$rec){
            $rec_list_item['id'] = $rec["id"];
            $rec_list_item['supcategory_id'] = $rec["supcategory_id"];
            $rec_list_item['subcategory_id'] = $rec["subcategory_id"];
            $rec_list_item['title'] = SubCategory::getCategoryNameById($rec['subcategory_id']);
            array_push($rec_list,$rec_list_item);
        }
        return $rec_list;
    }
    public static  function getQuotaListBySubCateId($subCateId){
        $data = self::where('subcategory_id','=',$subCateId)
            ->where('flag','=',1)->select();

        $ret = [];

        if($data){
            foreach ($data as &$item){
                $tmp = [];
                $tmp["level_id"] = $item["level"];
                $tmp["category_name"] = CategoryService::getCategoryAndLevelNamesBySubCateId($item['subcategory_id'],$item["level"]);

                $tmp["fee_list"] =  QuotaService::getFeeList($item->staffitems);
                array_push($ret,$tmp);
            }
        }


        return $ret;

    }
    public static function getOneByOptions($supcategory_id,$subcategory_id,$level){
        $data = self::where('supcategory_id','=',$supcategory_id)
            ->where('subcategory_id','=',$subcategory_id)
            ->where("level","=",$level)->find();

        $total_fee = 0;
        $staff_fee = 0;
        $service_fee = 0;
        $achievement_fee = 0;
        $ret['standrad'] = $data['standrad'];
        $ret['scope'] = $data['scope'];
        $ret['material'] = $data['material'];
        $items = [];
        foreach ($data->staffitems as $item){
            $tmp_item = [];
            $tmp_item['fee_name'] = $item['fee_name'];
            $tmp_item['fee_num'] = $item['fee_num'];
            $tmp_item['fee_price'] = $item['fee_price'];
            if($item["type"] == 0){
                $item["type"] = "人员费用";
                $tmp_item['type'] = $item["type"];
                $staff_fee =  $staff_fee+floatval($item["fee_price"])*$item["fee_num"];
            }
            if($item["type"] == 1){
                $item["type"] = "服务费用";
                $tmp_item['type'] = $item["type"];
                $service_fee = $service_fee+ floatval($item["fee_price"])*$item["fee_num"];
            }
            if($item["type"] == 2){
                $item["type"] = "业绩费用";
                $tmp_item['type'] = $item["type"];
                $achievement_fee =$achievement_fee+floatval($item["fee_price"])*$item["fee_num"];
            }

//
            $item["item_total"] = floatval($item["fee_price"])*$item["fee_num"];
            $total_fee = $total_fee+$item["item_total"];
            $tmp_item['item_total'] = $item["item_total"];
            array_push($items,$tmp_item);

        }
        $ret["items"] = $items;
        $ret["staff_fee"] = $staff_fee;
        $ret["service_fee"] = $service_fee;
        $ret["achievement_fee"] = $achievement_fee;
        $ret["total_fee"] = $total_fee;
        $ret["category_name"] = CategoryService::getCategoryAndLevelNamesBySubCateId($subcategory_id,$level);


        return $ret;

    }

    public static function getMutilByOptions($opt){
        $opt_arr = explode(',',$opt);
        $ret = ['standrads'=>[],'materials'=>[],'items'=>[],'staff_fee'=>null,'service_fee'=>null,'achievement_fee'=>null,'total_fee'=>null,'category_names'=>[]];
        $_tmp_arr = [];
        $staff_fee = 0;
        $service_fee = 0;
        $service_fee_arr = array();
        $service_fee_num_arr = array();
        $achievement_fee = 0;
        $achievement_fee_arr = array();
        $achievement_fee_num_arr = array();
        $person_num = 0;
        $service_num = 0;
        $achievement_num = 0;
        $complete_name = '';
        $classification_name = '';
        foreach ($opt_arr as $k=>$o){
            $o_arr = explode("-",$o);
            $supcategory_id = $o_arr[0];
            $subcategory_id = $o_arr[1];
            $level = $o_arr[2];
            $data = self::where('supcategory_id','=',$supcategory_id)
                ->where('subcategory_id','=',$subcategory_id)
                ->where("level","=",$level)->find();
            $ret['standrads'][$k] = $data['standrad'];
            $ret['materials'][$k] = $data['material'];
            $ret["category_names"][$k] = CategoryService::getCategoryAndLevelNamesBySubCateId($subcategory_id,$level);
            $supcategory_name = SupCategory::getCategoryNameById($supcategory_id);
            $subcategory_name = SubCategory::getCategoryNameById($subcategory_id);
            $level_name       = Level::getLevelNameById($level);
            if(empty($complete_name)) {
                $complete_name .= "[" . $supcategory_name . "-" . $subcategory_name . "-" . $level_name."]";
            }else{
                $complete_name .= "/[" . $supcategory_name . "-" . $subcategory_name . "-" . $level_name."]";
            }
            if(empty($classification_name)) {
                $classification_name .= "[" . $supcategory_name . "-" . $subcategory_name ."]";
            }else{
                $classification_name .= "/[" . $supcategory_name . "-" . $subcategory_name ."]";
            }
            foreach ($data->staffitems as $item) {
                $item['fee_alias'] = empty($item['fee_alias']) ? $item['fee_name'] : $item['fee_alias'];
                $_key =$item['fee_name'].'_'.$item['fee_alias'];
                if($item["type"] == 0 && !array_key_exists($_key,$_tmp_arr)){
                    $_staff_fee = round(floatval($item["fee_price"])*$item["fee_num"],2);
                    $_staff_fee_max = isset($_tmp_arr[$_key]) && !empty($_tmp_arr[$_key]) ? $_tmp_arr[$_key] : 0;
                    if($_staff_fee > $_staff_fee_max) {
                        $_tmp_arr[$_key] = $_staff_fee;
                        $staff_fee += $_staff_fee - $_staff_fee_max;
                        $ret['items']['person'][$item["fee_name"]][$_key]['type']          = "人员费用";
                        $ret['items']['person'][$item["fee_name"]][$_key]['fee_name']     = $item["fee_name"];
                        $ret['items']['person'][$item["fee_name"]][$_key]['fee_alias']    = $item["fee_alias"];
                        $ret['items']['person'][$item["fee_name"]][$_key]['fee_num']      = $item["fee_num"];
                        $ret['items']['person'][$item["fee_name"]][$_key]['fee_price']    = $item["fee_price"];
                        $ret['items']['person'][$item["fee_name"]][$_key]['item_total']   = $_staff_fee;
                        $person_num++;
                    }
                }
                if($item["type"] == 1){
                    if($service_fee >= 7 ){
                        $_service_fee = 1.5;
                        $service_fee += $_service_fee;
                    }else {
                        $_service_fee = round(floatval($item["fee_price"]) * $item["fee_num"], 2);
                        $service_fee += $_service_fee;
                    }
                    if(!isset( $service_fee_arr[$_key]) ){
                        $service_num++;
                    }
                    $service_fee_arr[$_key] = isset( $service_fee_arr[$_key]) ? $service_fee_arr[$_key] + $_service_fee : $_service_fee;
                    $service_fee_num_arr[$_key] = isset( $service_fee_num_arr[$_key]) ? $service_fee_num_arr[$_key] + $item["fee_num"] :  $item["fee_num"];
                    $ret['items']['service'][$item["fee_name"]][$_key]['type']           = "服务费用";
                    $ret['items']['service'][$item["fee_name"]][$_key]['fee_name']      = $item["fee_name"];
                    $ret['items']['service'][$item["fee_name"]][$_key]['fee_alias']     = $item["fee_alias"];
                    $ret['items']['service'][$item["fee_name"]][$_key]['fee_num']       =  $service_fee_num_arr[$_key] ;
                    $ret['items']['service'][$item["fee_name"]][$_key]['fee_price']     = $service_fee_arr[$_key];
                    $ret['items']['service'][$item["fee_name"]][$_key]['item_total']    = $service_fee_arr[$_key];
                }
                if($item["type"] == 2){
                    $_achievement_fee = round(floatval($item["fee_price"])*$item["fee_num"],2);
                    $achievement_fee += $_achievement_fee;
                    if(!isset( $achievement_fee_arr[$_key]) ){
                        $achievement_num++;
                    }
                    $achievement_fee_arr[$_key] = isset( $achievement_fee_arr[$_key]) ? $achievement_fee_arr[$_key] + $_achievement_fee : $_achievement_fee;
                    $achievement_fee_num_arr[$_key] = isset( $achievement_fee_num_arr[$_key]) ? $achievement_fee_num_arr[$_key] + $item["fee_num"] :  $item["fee_num"];
                    $ret['items']['achievement'][$item["fee_name"]][$_key]['type']          = "业绩费用";
                    $ret['items']['achievement'][$item["fee_name"]][$_key]['fee_name']     = $item["fee_name"];
                    $ret['items']['achievement'][$item["fee_name"]][$_key]['fee_alias']    = $item["fee_alias"];
                    $ret['items']['achievement'][$item["fee_name"]][$_key]['fee_num']      = $achievement_fee_num_arr[$_key] ;
                    $ret['items']['achievement'][$item["fee_name"]][$_key]['fee_price']    = $achievement_fee_arr[$_key];
                    $ret['items']['achievement'][$item["fee_name"]][$_key]['item_total']   = $achievement_fee_arr[$_key];
                }
            }
        }
        $ret['staff_fee'] = $staff_fee;
        $ret['service_fee'] = $service_fee;
        $ret['achievement_fee'] = $achievement_fee;
        $ret['person_num'] = $person_num;
        $ret['service_num'] = $service_num;
        $ret['achievement_num'] = $achievement_num;
        $ret['complete_name'] = $complete_name;
        $ret['classification_name'] = $classification_name;
        $ret['total_fee'] = round($staff_fee+$service_fee+ $achievement_fee,2);
        return $ret;

    }


}